{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "f43d8540-cb54-4623-9c19-e49759f849f2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.89\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tushare as ts\n",
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "print(ts.__version__)\n",
    "pro = ts.pro_api('ea3263c5424f08c3e04d605af9458cfc349613d5b7c27e99994eb396')\n",
    "#pro = ts.pro_api('ea3263c5424f08c3e04d605af9458cfc349613d5b7c27e99994eb396')\n",
    "def get_daily_data(ts_code, start, end):\n",
    "    return pro.daily(ts_code=ts_code.upper(), start_date=start, end_date=end)\n",
    "\n",
    "holdings={\n",
    "    \"格力电器\":\"000651.sz\",\n",
    "    \"伊利股份\":\"600887.sh\",\n",
    "    \"三峡能源\":\"600905.sh\",\n",
    "    \"中国银行\":\"601988.sh\",\n",
    "    \"光大银行\":\"601818.sh\",\n",
    "    \"万科A\":\"000002.sz\",\n",
    "    \"东方财富\":\"300059.sz\",\n",
    "    \"乐普医疗\":\"300003.sz\",\n",
    "    \"奕东电子\":\"301123.sz\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "aa738573-7f29-4837-b4bd-5c74c7a0cbd0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "From 20221107 to 20230508.\n"
     ]
    }
   ],
   "source": [
    "# Automatic get start date and end date, from half year ago.\n",
    "import datetime\n",
    "from dateutil.relativedelta import relativedelta\n",
    "current_time=datetime.datetime.now()\n",
    "start=(current_time+datetime.timedelta(weeks = -26)).strftime('%Y%m%d')\n",
    "end=current_time.strftime('%Y%m%d')\n",
    "print(\"From %s to %s.\"%(start,end))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "a70818f0-dcbc-4532-88f2-4ccc4d5bfcfe",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>20230505</td>\n",
       "      <td>14.95</td>\n",
       "      <td>15.64</td>\n",
       "      <td>14.91</td>\n",
       "      <td>15.54</td>\n",
       "      <td>14.95</td>\n",
       "      <td>0.59</td>\n",
       "      <td>3.9465</td>\n",
       "      <td>1495445.04</td>\n",
       "      <td>2297283.239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000651.SZ</td>\n",
       "      <td>20230505</td>\n",
       "      <td>35.40</td>\n",
       "      <td>35.88</td>\n",
       "      <td>34.77</td>\n",
       "      <td>35.15</td>\n",
       "      <td>35.42</td>\n",
       "      <td>-0.27</td>\n",
       "      <td>-0.7623</td>\n",
       "      <td>1108025.09</td>\n",
       "      <td>3900066.284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>300003.SZ</td>\n",
       "      <td>20230505</td>\n",
       "      <td>24.05</td>\n",
       "      <td>24.10</td>\n",
       "      <td>23.46</td>\n",
       "      <td>23.80</td>\n",
       "      <td>23.95</td>\n",
       "      <td>-0.15</td>\n",
       "      <td>-0.6263</td>\n",
       "      <td>111344.62</td>\n",
       "      <td>263856.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>300059.SZ</td>\n",
       "      <td>20230505</td>\n",
       "      <td>15.99</td>\n",
       "      <td>16.40</td>\n",
       "      <td>15.91</td>\n",
       "      <td>16.00</td>\n",
       "      <td>15.97</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.1879</td>\n",
       "      <td>3055661.85</td>\n",
       "      <td>4928202.484</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>301123.SZ</td>\n",
       "      <td>20230505</td>\n",
       "      <td>22.42</td>\n",
       "      <td>22.42</td>\n",
       "      <td>21.94</td>\n",
       "      <td>22.18</td>\n",
       "      <td>22.44</td>\n",
       "      <td>-0.26</td>\n",
       "      <td>-1.1586</td>\n",
       "      <td>10895.91</td>\n",
       "      <td>24141.617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1075</th>\n",
       "      <td>301123.SZ</td>\n",
       "      <td>20221107</td>\n",
       "      <td>24.35</td>\n",
       "      <td>24.63</td>\n",
       "      <td>24.10</td>\n",
       "      <td>24.30</td>\n",
       "      <td>24.29</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.0412</td>\n",
       "      <td>19665.58</td>\n",
       "      <td>47823.870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1076</th>\n",
       "      <td>600887.SH</td>\n",
       "      <td>20221107</td>\n",
       "      <td>27.49</td>\n",
       "      <td>28.13</td>\n",
       "      <td>27.22</td>\n",
       "      <td>27.71</td>\n",
       "      <td>27.73</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.0721</td>\n",
       "      <td>548725.89</td>\n",
       "      <td>1520203.544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1077</th>\n",
       "      <td>600905.SH</td>\n",
       "      <td>20221107</td>\n",
       "      <td>5.73</td>\n",
       "      <td>5.73</td>\n",
       "      <td>5.67</td>\n",
       "      <td>5.70</td>\n",
       "      <td>5.71</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>-0.1751</td>\n",
       "      <td>1165779.85</td>\n",
       "      <td>664460.807</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1078</th>\n",
       "      <td>601818.SH</td>\n",
       "      <td>20221107</td>\n",
       "      <td>2.78</td>\n",
       "      <td>2.79</td>\n",
       "      <td>2.77</td>\n",
       "      <td>2.78</td>\n",
       "      <td>2.78</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>413511.72</td>\n",
       "      <td>114980.223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1079</th>\n",
       "      <td>601988.SH</td>\n",
       "      <td>20221107</td>\n",
       "      <td>3.04</td>\n",
       "      <td>3.04</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.04</td>\n",
       "      <td>3.04</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>731082.47</td>\n",
       "      <td>222023.591</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1080 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "0     000002.SZ   20230505  14.95  15.64  14.91  15.54      14.95    0.59   \n",
       "1     000651.SZ   20230505  35.40  35.88  34.77  35.15      35.42   -0.27   \n",
       "2     300003.SZ   20230505  24.05  24.10  23.46  23.80      23.95   -0.15   \n",
       "3     300059.SZ   20230505  15.99  16.40  15.91  16.00      15.97    0.03   \n",
       "4     301123.SZ   20230505  22.42  22.42  21.94  22.18      22.44   -0.26   \n",
       "...         ...        ...    ...    ...    ...    ...        ...     ...   \n",
       "1075  301123.SZ   20221107  24.35  24.63  24.10  24.30      24.29    0.01   \n",
       "1076  600887.SH   20221107  27.49  28.13  27.22  27.71      27.73   -0.02   \n",
       "1077  600905.SH   20221107   5.73   5.73   5.67   5.70       5.71   -0.01   \n",
       "1078  601818.SH   20221107   2.78   2.79   2.77   2.78       2.78    0.00   \n",
       "1079  601988.SH   20221107   3.04   3.04   3.03   3.04       3.04    0.00   \n",
       "\n",
       "      pct_chg         vol       amount  \n",
       "0      3.9465  1495445.04  2297283.239  \n",
       "1     -0.7623  1108025.09  3900066.284  \n",
       "2     -0.6263   111344.62   263856.000  \n",
       "3      0.1879  3055661.85  4928202.484  \n",
       "4     -1.1586    10895.91    24141.617  \n",
       "...       ...         ...          ...  \n",
       "1075   0.0412    19665.58    47823.870  \n",
       "1076  -0.0721   548725.89  1520203.544  \n",
       "1077  -0.1751  1165779.85   664460.807  \n",
       "1078   0.0000   413511.72   114980.223  \n",
       "1079   0.0000   731082.47   222023.591  \n",
       "\n",
       "[1080 rows x 11 columns]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=get_daily_data(ts_code,start,end)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1406f0fa-633d-41a4-8093-e3579c9b016a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
